/**
 * @file Simicircle.as
 *  
 半圆形图片浏览
 * @author db0@qq.com
 * @version 1.0.1
 * @date 2017-10-28
 */
package 
{
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;

	public class Simicircle extends Sprite
	{
		private var r:int;
		private var arr:Array;
		public function Simicircle(urlArr:Array,_r:int=50,num:int=5)
		{
			r=_r;
			maxPhotos = num;
			arr = urlArr;
			if(arr && arr.length>0)
			{
				addItem(arr[0]);
				var i:int = 0;
				while(i<num)
				{
					var h:int = i+1;
					while(h>=arr.length)
						h-=arr.length;
					addItem(arr[h],true);
					h = -i-1;
					while(h<0)
						h+=arr.length;
					addItem(arr[h],false);
					++i;
				}
			}

			index = 0;
			MyKey.main.addEventListener(MyKey.RIGHT,next);
			MyKey.main.addEventListener(MyKey.LEFT,prev);
		}

		private var index:int = 0;
		public function next(e:Event):void
		{
			if(!visible)
				return;
			++index;
			if(index>=arr.length)
				index = 0;

			addItem(arr[index],true);
		}
		public function prev(e:Event):void
		{
			if(!visible)
				return;
			--index;
			if(index<0)
				index = arr.length-1;

			addItem(arr[index],false);
		}




		private var maxPhotos:int = 1;
		private var headAngle:Number=0;
		private var railAngle:Number=0;
		private function get eachAngle():Number
		{
			return Math.PI/(1+maxPhotos)/2;
		}

		private function addItem(url:String,_head:Boolean=true):void
		{
			var mc:PhotoLoader = new PhotoLoader(url);
			if(numChildren==0)
			{
				headAngle = railAngle = Math.PI/2;
				railAngle += eachAngle;
			}
			if(_head){
				mc.x = r*Math.cos(headAngle);
				mc.y = -r*Math.sin(headAngle);
				mc.rotation = 90-headAngle*180/Math.PI;//-Math.PI/2;
				headAngle -= eachAngle;
				addChildAt(mc,0);
			}else{
				mc.x = r*Math.cos(railAngle);
				mc.y = -r*Math.sin(railAngle);
				mc.rotation = 90-railAngle*180/Math.PI;//-Math.PI/2;
				railAngle += eachAngle;
				addChild(mc);
			}
			logs.adds(mc.rotation/Math.PI*180);
			if(numChildren>maxPhotos*2+1)
			{
				if(_head){
					removeChildAt(numChildren-1);
					railAngle -= eachAngle
				}else{
					removeChildAt(0);
					headAngle += eachAngle
				}
			}
			var m:PhotoLoader = middle;
			if(m)
			{
				//TweenLite.to(this,.1,{rotation:-m.rotation});
				rotation =-m.rotation;
				if(stage)
				{
					var bmp:Bitmap = new Bitmap(m.bmp.bitmapData);
					ViewSet.fullCenter(bmp,0,0,Main.stageWidth,Main.stageWidth);
					stage.addChild(bmp);
				}
			}
		}
		private function get middle():PhotoLoader
		{
			if(numChildren==maxPhotos*2+1)
			{
				return getChildAt(maxPhotos) as PhotoLoader;
			}
			return null;
		}
	}
}
